import time
import socket
import pickle
from threading import Thread
import pos_nokov as pos
from concurrent.futures import ThreadPoolExecutor
from position import *
import copy


def server_send(new_socket, gp, gv):
    while True:
        p=copy.deepcopy(gp) # 必须copy，否则传递的只是个指针
        v=copy.deepcopy(gv)
        new_socket.send(pickle.dumps(v))
        time.sleep(0.01)
        new_socket.send(pickle.dumps(p))
        time.sleep(0.01)

def run(p, v):
    pool=ThreadPoolExecutor(max_workers=3)

    server_addr=('192.168.3.5', 7788)
    server_socket=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    server_socket.bind(server_addr)
    print(f'tcp server started at {server_addr}')
    while True:
        new_socket, addr=server_socket.accept()
        print(f'received req from {addr}')
        pool.submit(server_send, new_socket, p, v)

if __name__=='__main__':
    
    run(poses(), speeds())